<?php

class Jhg_TwigExtention_Base extends Twig_Extension {

    private $_environment;

    public function initRuntime(Twig_Environment $environment) {
        $this->_environment = $environment;
    }

    public function getName() {
        return 'bm';
    }

    public function getGlobals() {
        $auth = Zend_Auth::getInstance();
        $isUser = $auth->hasIdentity();
        $isAdmin = $isUser && $auth->getIdentity()->login === 'admin';
        $userData = null;
//        if ($isUser) {
//            $userData = new Kp2_Model_UserData();
//        }
        return array(
            //'isDevEnv' => 'development' === APPLICATION_ENV,
            'isUser' => $isUser,
            'isAdmin' => $isAdmin,
            //'session' => Zend_Registry::get('session'),
            'user' => $userData,
        );
    }

    public function getFilters() {
        return array(
            'plural' => new Twig_Filter_Method($this, 'filterPlural'),
            'ago' => new Twig_Filter_Method($this, 'filterAgo'),
            'nl2br' => new Twig_Filter_Function('nl2br'),
            'mb_strlen' => new Twig_Filter_Function('mb_strlen'),
            'mb_lower' => new Twig_Filter_Function('mb_strtolower'),
            'substring' => new Twig_Filter_Method($this, 'functionSubstring'),
            'mb_truncate' => new Twig_Filter_Method($this, 'filterMbTruncate'),
        );
    }

    public function getFunctions() {
        return array(
            'mb_strlen' => new Twig_Function_Function('mb_strlen'),
            'substring' => new Twig_Function_Method($this, 'functionSubstring'),
            'url' => new Twig_Function_Method($this, 'functionUrl'),
        );
    }

    /* Filters */

    public function filterPlural($num, $type) {
        $translator = Zend_Registry::get('Zend_Translate');
        return $num . ' ' . $translator->plural($type, null, $num);
    }

    public function filterAgo($date, $isFull = true) {
        $filter = new Jh_Filter_Ago($isFull);
        return $filter->filter($date);
    }

    public function filterMbTruncate($string, $length = 80, $etc = '...', $charset = 'UTF-8', $break_words = false, $middle = false) {
        if ($length == 0)
            return '';

        if (mb_strlen($string, $charset) > $length) {
            $length -= min($length, mb_strlen($etc, $charset));
            if (!$break_words && !$middle) {
                $string = preg_replace('/\s+?(\S+)?$/', '', mb_substr($string, 0, $length + 1, $charset));
            }
            if (!$middle) {
                return mb_substr($string, 0, $length, $charset) . $etc;
            } else {
                return mb_substr($string, 0, $length / 2, $charset) . $etc . mb_substr($string, -$length / 2, $charset);
            }
        } else {
            return $string;
        }
    }

    /* Functions */

    function functionSubstring($string, $position, $length) {
        return mb_substr($string, $position - 1, $length, 'UTF-8');
    }

    public function functionUrl($name = null, array $urlOptions = array(), $reset = false, $encode = true) {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        return $router->assemble($urlOptions, $name, $reset, $encode);
    }

}
